/**
 *
 */
package de.cbf.cam.dsa.item.ui.composites;

import java.util.Arrays;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import de.cbf.cam.dsa.item.interfaces.ICompositeEditShapeness;

/**
 * @author salter
 *
 */
public class CompositeEditShapeness extends AbstractCompositeEdit implements ICompositeEditShapeness {

	private static final String TEXT_BT_ADD 	= "↓ ↓ ↓";
	private static final String TEXT_BT_REMOVE 	= "X";
	private static final String TEXT_BT_UP 		= "↑";
	private static final String TEXT_BT_DOWN 	= "↓";

	protected org.eclipse.swt.widgets.List listLevel;

	protected Text 	 txLevel;

	protected Button btRemove;
	protected Button btDown;
	protected Button btUp;
	protected Button btAdd;



	/**
	 * @param parent
	 * @param style
	 */
	public CompositeEditShapeness(Composite parent, int style) {
		super(parent, style);
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.item.ui.composites.AbstractCompositeEdit#buildComposite(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void buildComposite(Composite thisComposite) {
		GridLayout glMainLayout = new GridLayout(2,false);
		glMainLayout.marginWidth  = 0;
		glMainLayout.marginHeight = 0;

		//add textfield to add((modificaton makes add possible)
		txLevel = new Text(thisComposite, SWT.NONE);
		GridData gdLevelText = new GridData(SWT.FILL,SWT.CENTER,true,false,2,1);
		txLevel.setLayoutData(gdLevelText);
		//add button add
		btAdd = new Button(thisComposite, SWT.NONE);
		GridData gdBtAdd = new GridData(SWT.FILL,SWT.CENTER,true,false,2,1);
		btAdd.setLayoutData(gdBtAdd);
		btAdd.setText(TEXT_BT_ADD);

		//add a list(swt) (list selection for possible remove,up,down)
		listLevel = new org.eclipse.swt.widgets.List(thisComposite, SWT.NONE);
		GridData gdLevelList = new GridData(SWT.FILL,SWT.FILL,false,true,1,3);
		listLevel.setLayoutData(gdLevelList);

		//add button up
		btUp = new Button(thisComposite, SWT.NONE);
		GridData gdBtUp = new GridData(SWT.CENTER,SWT.CENTER,true,false,1,1);
		btUp.setLayoutData(gdBtUp);
		btUp.setText(TEXT_BT_UP);
		//add button down
		btDown = new Button(thisComposite, SWT.NONE);
		GridData gdBtDown = new GridData(SWT.CENTER,SWT.CENTER,true,false,1,1);
		btDown.setLayoutData(gdBtDown);
		btDown.setText(TEXT_BT_DOWN);

		//add button remove
		btRemove = new Button(thisComposite, SWT.NONE);
		GridData gdBtRemove = new GridData(SWT.CENTER,SWT.CENTER,true,false,1,1);
		btRemove.setLayoutData(gdBtRemove);
		btRemove.setText(TEXT_BT_REMOVE);
	}

	@Override
	public List<String> getShapenessLevels() {
		String[] items = listLevel.getItems();
		return Arrays.asList(items);
	}

	@Override
	public void setShapenessLevels(List<String> levels) {
		int selectionIndex = listLevel.getSelectionIndex();
		String[] items = levels.toArray(new String[levels.size()]);
		listLevel.setItems(items);
		listLevel.setSelection(selectionIndex);
	}

	@Override
	public String getLevelText() {
		return txLevel.getText();
	}

	@Override
	public void setLevelText(String text) {
		txLevel.setText(text);
	}

	@Override
	public String getSelectedLevel() {
		String result = "";
		int index = listLevel.getSelectionIndex();
		if(index >= 0){
			result = listLevel.getItem(index);
		}
		return result;
	}

	@Override
	public void setSelectedLevel(String text) {
		int index = listLevel.indexOf(text);
		listLevel.setSelection(index);
	}

	@Override
	public void setActivatedButtonAdd(boolean activated) {
		btAdd.setEnabled(activated);
	}

	@Override
	public void setActivatedButtonRemove(boolean activated) {
		btRemove.setEnabled(activated);
	}

	@Override
	public void setActivatedButtonDown(boolean activated) {
		btDown.setEnabled(activated);
	}

	@Override
	public void setActivatedButtonUp(boolean activated) {
		btUp.setEnabled(activated);
	}

}
